<template>
  <view>
    <view class="goods-list">
      <view v-for="(goods, index) in goodsList" :key="index" @click="gotoDetail(goods)">
        <!-- 为my-goods组件动态绑定 goods 属性的值 -->
        <my-goods :goods="goods"></my-goods>
      </view>
    </view>
  </view>
</template>

<script>
  export default {
    onLoad(options) {
      this.queryObj.query = options.query || '';
      this.queryObj.cid = options.cid || '';
      this.getGoodsList();
    },
    methods: {
      async getGoodsList(cb) {
        //打开节流阀
        this.isLoading = true;
        // 发起请求
        const { data: res} = await uni.$http.get('/api/public/v1/goods/search', this.queryObj);
        //关闭节流阀
        this.isLoading = false;
        // 调用回调函数
        cb && cb();
        //判断请求是否成功
        if (res.meta.status !== 200) return uni.$showMsg();
        this.total = res.message.total;
        this.goodsList = [...this.goodsList,...res.message.goods]
      },
      //触底事件
      onReachBottom(){
        //判断是否正在请求数据
        if(this.isLoading) return;
        //判断是否还有数据
        if(this.queryObj.pagenum * this.queryObj.pagesize >= this.total) return uni.$showMsg('数据加载完毕');
        this.queryObj.pagenum++;
        this.getGoodsList();
      },
      //下拉刷新事件
      onPullDownRefresh(){
        //重置关键数据
        this.total = 0;
        this.queryObj.pagenum = 1;
        this.goodsList = [];
        //重新发起请求
        this.getGoodsList(() =>uni.stopPullDownRefresh());
      },
      //点击事件
      gotoDetail(goods){
        uni.navigateTo({
          url: '/subpkg/goods_detail/goods_detail?goods_id=' + goods.goods_id
        })
      }
      
      
    },
    data() {
      return {
        // 默认空图片
        defaultPic: 'https://img3.doubanio.com/f/movie/8dd0c794499fe925ae2ae89ee30cd225750457b4/pics/movie/celebrity-default-medium.png',
        //请求参数对象
        queryObj: {
          query: '',
          cid: '',
          pagenum: 1,
          pagesize: 10
        },
        goodsList: [],
        total: 0,
        //是否正在请求数据
        isLoading: false


      }
    }
  }
</script>

<style lang="scss">
  
</style>